﻿@model IEnumerable<JustMVCTest.Models.PO>
@using JustMVCTest.Models
@{
    ViewBag.Title = "History";
}

<font size="3" color="0066CC"> <b> History Details for  PO Number: @Html.DisplayFor(modelItem => modelItem.First().PONumber) Item Number: @Html.DisplayFor(modelItem => modelItem.First().Item) </b> </font>

  <br/>  <br/>


 @(Html.Telerik().Grid<PO>(Model)
        .Name("POesGrid")
        .DataKeys(keys =>
        {
            keys.Add(p => p.PONumber);
            keys.Add(p => p.Item);
            keys.Add(p => p.Update_Num);
        })
        .ToolBar(commands => commands
                    .Custom()
                    .HtmlAttributes(new { id = "exportTableToCSV" })
                    .Text("Export table to CSV")
                              .Action("HistoryPOsExportTableToCsv", "History", new { Item = Request.QueryString["Item"], PONumber = Request.QueryString["PONumber"] }))
        .Columns(columns =>
        {

            columns.Bound(o => o.Update_Num).Width(100).Sortable(false);
            columns.Bound(o => o.Update_Date).Format("{0:dd/MM/yyyy HH:mm:ss}").Width(180).Sortable(false);
            columns.Bound(o => o.Descr).Width(300).Sortable(false);
            columns.Bound(o => o.DeliveryDate).Width(150).Format("{0:d}").Sortable(false);

            columns.Bound(o => o.Status).Width(200).Sortable(false);
            columns.Bound(o => o.Requisitioner).Width(150).Sortable(false);

            columns.Bound(o => o.DeliveryDate_Req).Width(200).Sortable(false);

            columns.Bound(o => o.Cost_Cent).Width(150).Sortable(false);
            columns.Bound(o => o.Int_Order).Width(150).Sortable(false);
            columns.Bound(o => o.Prof_Cent).Width(150).Sortable(false);
            columns.Bound(o => o.Percentage).Width(150).Sortable(false);
            columns.Bound(o => o.Percentage_Update_Date).Format("{0:dd/MM/yyyy HH:mm:ss}").Width(180).Sortable(false);
            columns.Bound(o => o.cost_elem).Width(150).Sortable(false);
            columns.Bound(o => o.co_el_name).Width(200).Sortable(false);
            columns.Bound(o => o.Vendor).Width(100).Sortable(false);
            columns.Bound(o => o.VendorName).Width(300).Sortable(false);
            columns.Bound(o => o.UM).Width(100).Sortable(false);
            columns.Bound(o => o.T_Curr).Width(100).Sortable(false);
            columns.Bound(o => o.Sch_Lin).Width(100).Sortable(false);
            columns.Bound(o => o.Ord_Qty).Width(100).Sortable(false);
            columns.Bound(o => o.Open_Qty).Width(100).Sortable(false);
            columns.Bound(o => o.Open_Amt_T).Width(100).Sortable(false);
            columns.Bound(o => o.Order_Amt).Width(100).Sortable(false);
            columns.Bound(o => o.Open_Amt_L).Width(100).Sortable(false);
            columns.Bound(o => o.TSSC_Pending_Send_Date).Width(100).Sortable(false);
            columns.Bound(o => o.Purchase_O).Width(100).Sortable(false);

        })

        .CellAction(cell =>
        {
            if (cell.Column.Member == "Status")
            {
                PO po = cell.DataItem;
            }
        })


        .Pageable()
        .Scrollable(c => c.Height("320px"))
        .Resizable(resizing => resizing.Columns(true))

        .CellAction(cell =>
        {

            if (cell.DataItem.Update_Num > 1)
            {
                
                PODBContext dbx = new PODBContext();

                var previous_data = 
                    (from pos in dbx.POs
                     where (pos.PONumber == cell.DataItem.PONumber && pos.Item == cell.DataItem.Item && pos.Update_Num == cell.DataItem.Update_Num - 1)
                     select new { pos.Percentage_Update_Date, pos.Percentage, pos.DeliveryDate, pos.DeliveryDate_Req, pos.Ord_Qty, pos.Open_Qty, pos.Open_Amt_T, pos.Order_Amt, pos.Open_Amt_L, pos.TSSC_Pending_Send_Date, pos.Status });

                if (cell.Column.Member == "Percentage_Update_Date" && cell.DataItem.Percentage_Update_Date != previous_data.First().Percentage_Update_Date)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Percentage" && cell.DataItem.Percentage != previous_data.First().Percentage )
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "DeliveryDate" && cell.DataItem.DeliveryDate != previous_data.First().DeliveryDate)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "DeliveryDate_Req" && cell.DataItem.DeliveryDate_Req != previous_data.First().DeliveryDate_Req)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Ord_Qty" && cell.DataItem.Ord_Qty != previous_data.First().Ord_Qty)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Open_Qty" && cell.DataItem.Open_Qty != previous_data.First().Open_Qty)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Open_Amt_T" && cell.DataItem.Open_Amt_T != previous_data.First().Open_Amt_T)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Order_Amt" && cell.DataItem.Order_Amt != previous_data.First().Order_Amt)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Open_Amt_L" && cell.DataItem.Open_Amt_L != previous_data.First().Open_Amt_L)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "TSSC_Pending_Send_Date" && cell.DataItem.TSSC_Pending_Send_Date != previous_data.First().TSSC_Pending_Send_Date)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

                if (cell.Column.Member == "Status" && cell.DataItem.Status != previous_data.First().Status)
                    cell.HtmlAttributes["style"] = "color:blue;font-weight:bold;";

            }

            
            if (cell.Column.Member == "Status")
            {
                switch (cell.DataItem.Status)
                {
                    case 0:
                        cell.Text = "Open";
                        break;
                    case 1:
                        cell.Text = "Pending Close";
                        break;
                    case 2:
                        cell.Text = "Pending Delivery Date";
                        break;
                    case 99:
                        cell.Text = "Closed";
                        break;
                }
            }

            if (cell.Column.Member == "DeliveryDate_Req")
            {
                if (cell.DataItem.DeliveryDate_Req == new DateTime(2000, 1, 1, 0, 0, 0) || cell.DataItem.Status != 2)
                {
                    cell.Text = " ";
                }
            }
        })        
        
        .Sortable(sorting => sorting
        .OrderBy(sortOrder => sortOrder.Add(o => o.Update_Num).Descending())
        )
             .Pageable(paging => paging.PageSize(11))
                  .Scrollable(c => c.Height("400px"))


    )

